from django.db import models

from BYD_ERP.utils.base_model import BaseModel, BaseModel2


# python ../../manage.py startapp goods_info

# 迁移基础：basic_info,

# python ../../manage.py makemigrations goods_info
# python ../../manage.py migrate goods_info

# python ../../manage.py check

# 货品类别模型类
class GoodsCategoryModel(BaseModel2):
    name = models.CharField(max_length=100, verbose_name='货品类别名称')  # 原材料、辅助材料、成品、齿形带、多楔带、同步带
    number_code = models.CharField('编号', max_length=28, unique=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    order_number = models.IntegerField('排序号码', default=100)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE)

    class Meta:
        db_table = 't_goods_category'
        verbose_name = '货品类别'
        verbose_name_plural = verbose_name
        ordering = ['order_number', 'id']

    def __str__(self):
        return self.name

# cylinder：圆筒，带筒
# 计量单位模型类
class UnitsModel(BaseModel2):
    basic_name = models.CharField('基本单位', max_length=20, unique=True)
    backup_name = models.CharField('副单位', max_length=20, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    class Meta:
        db_table = 't_units'
        verbose_name = '计量单位'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        # 100 千克（KG）
        return f'{self.basic_name}({self.backup_name})'


# 合作厂家（商标分类）模型类
class PartnerModel(BaseModel2):
    name = models.CharField('合作厂家简称', max_length=20, unique=True)
    full_name = models.CharField('合作厂家全称', max_length=50, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    class Meta:
        db_table = 't_partner'
        verbose_name = '合作厂家'
        verbose_name_plural = verbose_name
        ordering = ['id']


# 断面模型类
class SectionModel(BaseModel2):
    name = models.CharField('断面名称', max_length=20, unique=True)  # 6PK/8PK/10PK/AV13/AV17
    order_number = models.IntegerField('排序号码', default=1001)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    class Meta:
        db_table = 't_section'
        verbose_name = '断面'
        verbose_name_plural = verbose_name
        ordering = ['order_number', 'id']


# 附件模型类
class AttachmentModel(BaseModel):
    # 附件文件类型
    type_choices = (
        ('image', '图片'),
        ('pdf', 'PDF文档'),
        ('doc', 'Word文档'),
        ('excel', 'Excel文档'),
        ('zip', '压缩文件'),
        ('other', '其他文件')
    )
    """
        只要是choices参数的字段，如果想要获取对应信息，固定写法：get_字段名_display()
        print(Attachment.a_type)
        print(Attachment.get_a_type_display())
    """
    a_file = models.FileField('附件或者图片')
    a_type = models.CharField('附件的类型', max_length=20, blank=True, null=True, choices=type_choices)

    class Meta:
        db_table = 't_attachment'
        verbose_name = '附件表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.a_file.name


# 货品模型类
class GoodsModel(BaseModel2):
    name = models.CharField('货品国标名称', max_length=20)
    ###################################################
    # 货品编号：不重复，随着货品新增自动加壹
    goods_code = models.CharField('货品编号', max_length=28, unique=True)
    invoice_name = models.CharField('货品开票名称', max_length=20, blank=True, null=True)
    specification = models.CharField('规格/件号', max_length=50, blank=True, null=True)
    model_number = models.CharField('国标型号', max_length=50, blank=True, null=True)
    invoice_model_number = models.CharField('开票型号', max_length=50, blank=True, null=True)
    partner_id = models.ForeignKey('PartnerModel', related_name='partner_list', on_delete=models.SET_NULL, blank=True,
                                   null=True, verbose_name='合作厂家')
    # 冗余字段
    partner_name = models.CharField('合作方/厂家的名称', max_length=50, blank=True, null=True)
    brand = models.CharField('品牌', max_length=50, blank=True, null=True)
    basic_weight = models.CharField('基础重量', max_length=50, blank=True, null=True)
    length = models.CharField('型号长度mm', max_length=50, blank=True, null=True)
    control_length = models.CharField('控制尺寸/长度', max_length=50, blank=True, null=True)
    section_id = models.ForeignKey('SectionModel', related_name='section_list', on_delete=models.SET_NULL, blank=True,
                                   null=True, verbose_name='截面/断面')  # 6PK/8PK/10PK/AV13/AV15/AV17
    # 冗余字段
    section_name = models.CharField('截面/断面的名称', max_length=50, blank=True, null=True)  # 6PK/8PK/10PK/AV13/AV15/AV17
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    expiration_day = models.IntegerField('保质期', blank=True, null=True)
    market_price = models.DecimalField('市场价/统一价', max_digits=10, decimal_places=2, blank=True, null=True)
    negotiated_price = models.DecimalField('客户协议价', max_digits=10, decimal_places=2, blank=True, null=True)
    order_number = models.IntegerField('排序号码', default=100)
    units = models.ForeignKey('UnitsModel', on_delete=models.SET_NULL, blank=True, null=True, verbose_name='计量单位')
    goods_category = models.ForeignKey('GoodsCategoryModel', on_delete=models.SET_NULL, blank=True, null=True,
                                       verbose_name='货品类别')
    # 增加一个冗余字段
    goods_category_name = models.CharField('货品类别名称', max_length=30, blank=True, null=True)
    images_list = models.CharField('货品图片（实物拍照）所对应的ID列表', max_length=20, blank=True, null=True)  # 字段的值为：'1,2,3,4'
    brand_images_list = models.CharField('货品（型号）商标所对应的ID列表', max_length=20, blank=True, null=True)
    drawing_images_list = models.CharField('货品图纸所对应的ID列表', max_length=20, blank=True, null=True)

    class Meta:
        db_table = 't_goods'
        verbose_name = '货品表'
        verbose_name_plural = verbose_name
        ordering = ['order_number', 'id']

    def __str__(self):
        return self.name


# 货品库存模型类
class GoodsInventoryModel(models.Model):
    # 期初库存数量在创建好之后不能再修改
    init_inventory = models.DecimalField('期初库存数量', max_digits=10, decimal_places=2, default=0)
    cur_inventory = models.DecimalField('当前库存/指定日期库存数量', max_digits=10, decimal_places=2, default=0)
    lowest_inventory = models.DecimalField('最低安全库存，0表示不设置', max_digits=10, decimal_places=2, default=0)
    highest_inventory = models.DecimalField('最高安全库存，0表示不设置', max_digits=10, decimal_places=2, default=0)

    goods_id = models.ForeignKey('GoodsModel', related_name='goods_inventory_list', on_delete=models.CASCADE, blank=True,
                                 null=True, verbose_name='货品ID')
    warehouse_id = models.ForeignKey('basic_info.WarehouseModel', related_name='warehouse_inventory_list',
                                     on_delete=models.CASCADE, verbose_name='仓库ID')
    # 冗余字段，主要目的：减少联表查询的次数
    warehouse_name = models.CharField('仓库名称', max_length=50)

    class Meta:
        db_table = 't_goods_inventory'
        verbose_name = '货品库存表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.warehouse_name + str(self.id)

